package com.example.dllo.my36kr.ui.activity.sonactivity;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.dllo.my36kr.R;
import com.example.dllo.my36kr.db.DBTool;
import com.example.dllo.my36kr.model.CollectBean;
import com.example.dllo.my36kr.model.NewsChildBean;
import com.example.dllo.my36kr.net.SingleVolley;
import com.example.dllo.my36kr.net.VolleyResult;
import com.example.dllo.my36kr.tools.NewsUrls;
import com.example.dllo.my36kr.ui.activity.AbsBaseActivity;
import com.google.gson.Gson;
import com.squareup.picasso.Picasso;

import java.util.List;


/**
 * 新闻详情
 * Created by CPC_272 on 16/8/22.
 */
public class NewsChildActivity extends AbsBaseActivity implements View.OnClickListener, VolleyResult {
    private WebView news_web;
    private ImageView head_img, back_newsChild, collect_image;
    private TextView author_tv, title_newsChild;
    private ScrollView mScrollView;
    private LinearLayout goneLinearLayout;

    @Override
    protected int setLayout() {
        return R.layout.activity_child_news;
    }

    @Override
    protected void initView() {
        news_web = byView(R.id.web_new_view);
        head_img = byView(R.id.circleImage_newChild);
        author_tv = byView(R.id.author_newsChild);
        back_newsChild = byView(R.id.newsChild_back);
        title_newsChild = byView(R.id.title_newsChild);
        mScrollView = byView(R.id.scroll_newsChild);
        goneLinearLayout = byView(R.id.bottom_newsChild);
        collect_image = byView(R.id.favorite_collect_image);
    }

    @Override
    protected void initData() {
        showWebView();

        back_newsChild.setOnClickListener(this);
        collect_image.setOnClickListener(this);
        mScrollView.setOnScrollChangeListener(new View.OnScrollChangeListener() {
            @Override
            public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                //你先打LOG找到 你要做事情的位置 Y轴的值 找到你想要的位置
                Log.d("NewsChildActivity", "scrollY" + scrollY);
                if (scrollY >= 888) {
                    goneLinearLayout.setVisibility(View.GONE);
                    AlphaAnimation alphaAnimation = new AlphaAnimation(0.1f, 1.0f);
                    alphaAnimation.setDuration(1665);
                    alphaAnimation.setRepeatCount(1);
                    alphaAnimation.setRepeatMode(Animation.REVERSE);
                    goneLinearLayout.setAnimation(alphaAnimation);
                    alphaAnimation.start();
                } else {
                    goneLinearLayout.setVisibility(View.VISIBLE);
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.newsChild_back://新闻页进去的详情界面的返回
                finish();
            case R.id.favorite_collect_image:  //新闻页进去的详情页的收藏
                CollectBean collectBean = new CollectBean();
                Intent intent = getIntent();
                String tag = intent.getStringExtra("tag");
                collectBean.setTag(tag);

                String title = intent.getStringExtra("title");
                collectBean.setUseTitle(title);
                Log.d("NewsChildActivity", title);

                String picter = intent.getStringExtra("imageUrl");
                collectBean.setUsePic(picter);
                Log.d("NewsChildActivity", picter);


                String id = intent.getStringExtra("url");
                collectBean.setUrl(NewsUrls.ChildUrl + id);
                Log.d("NewsChildActivity", id);
                DBTool dbTool = DBTool.getInstance();
                dbTool.insertUser(collectBean);
                Toast.makeText(this, "收藏成功", Toast.LENGTH_SHORT).show();
                break;
        }
    }

    /**
     * webView的优化
     * WebSettings settings = webview.getSettings();
     * settings.setJavaScriptEnabled(true);//启用js
     * settings.setJavaScriptCanOpenWindowsAutomatically(true);//js和android交互
     * String cacheDirPath = PathCommonDefines.WEBVIEW_CACHE;
     * settings.setAppCachePath(cacheDirPath); //设置缓存的指定路径
     * settings.setAllowFileAccess(true); // 允许访问文件
     * settings.setAppCacheEnabled(true); //设置H5的缓存打开,默认关闭
     * settings.setUseWideViewPort(true);//设置webview自适应屏幕大小
     * settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);//设置，可能的话使所有列的宽度不超过屏幕宽度
     * settings.setLoadWithOverviewMode(true);//设置webview自适应屏幕大小
     * settings.setDomStorageEnabled(true);//设置可以使用localStorage
     * settings.setSupportZoom(false);//关闭zoom按钮
     * settings.setBuiltInZoomControls(false);//关闭zoom
     */
    //配置webView
    public void showWebView() {
        Intent intent = getIntent();
        String url = intent.getStringExtra("url");
        SingleVolley.getInstence(this).startRequest(url, this);
        /**
         * AndroidManifest.xml中必须使用许可"android.permission.INTERNET",否则会出Web page not available错误。
         * .如果访问的页面中有Javascript，则webview必须设置支持Javascript。
         *  webview.getSettings().setJavaScriptEnabled(true);
         */
        WebSettings webSettings = news_web.getSettings();
        webSettings.setJavaScriptEnabled(true);//支持JS脚本
        ////js和android交互
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        webSettings.setDefaultTextEncodingName("utf-8");//设置默认为utf-8
        //设置H5的缓存打开,默认关闭
        webSettings.setAppCacheEnabled(true);
        //设置缓存类型
        webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        //单列图片
        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
    }

    @Override
    public void success(String str, int type) {

    }

    @Override
    public void success(String str) {
        NewsChildBean newsChildBean = new Gson().fromJson(str, NewsChildBean.class);
        NewsChildBean.DataBean data = newsChildBean.getData();
        String content = data.getContent();
        title_newsChild.setText(data.getTitle());
        author_tv.setText(data.getUser().getName());
        Picasso.with(this).load(data.getUser().getAvatar()).into(head_img);
        //不转没法用webview解析
        //咱们那个不是网址  是js文件格式 全是尖括号  就得这么解析 别问我为什么 因为是甲鱼的臀部
        news_web.loadData(content, "text/html; charset=UTF-8", null);
    }

    @Override
    public void failure() {

    }
}
